﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Controler;
using MedicLibrary.Model;
using System.Data.SQLite;

namespace MedicLibrary.View
{
    public partial class frmDSThuoc : Form
    {
        
        public String action;

        public frmDSThuoc()
        {
            InitializeComponent();
        }

        private void frmDSThuoc_Load(object sender, EventArgs e)
        {
            dgvDSThuoc.DataSource = Thuoc.getDanhSachThuoc("");
        }

        private void dgvDSThuoc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.action == "LIST")
            {
                Session.IDThuoc = Int32.Parse(dgvDSThuoc.SelectedRows[0].Cells["ID"].Value.ToString());
                frmChiTietThuoc frmCTTHUOC = new frmChiTietThuoc();
                frmCTTHUOC.applyAction("VIEW");
                frmCTTHUOC.Show();
               
            }
            else if (this.action == "SEARCH")
            {   
                
            }
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmChiTietThuoc ctNhom = new frmChiTietThuoc();
            ctNhom.applyAction("ADD");
            ctNhom.ShowDialog();
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Session.IDThuoc = Int32.Parse(dgvDSThuoc.SelectedRows[0].Cells["ID"].Value.ToString());
            frmChiTietThuoc ctNhom = new frmChiTietThuoc();
            ctNhom.applyAction("EDIT");
            ctNhom.ShowDialog();
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Thuoc hc = new Thuoc();
            hc.ID = Int32.Parse(dgvDSThuoc.SelectedRows[0].Cells["ID"].Value.ToString());
            hc.deleteThuoc();

            dgvDSThuoc.DataSource = Thuoc.getDanhSachThuoc(txtTimKiem.Text);
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            dgvDSThuoc.DataSource = Thuoc.getDanhSachThuoc(txtTimKiem.Text);
        }

        private void btnXemHet_Click(object sender, EventArgs e)
        {
            dgvDSThuoc.DataSource = Thuoc.getDanhSachThuoc("xemtatcathuoc");
            txtTimKiem.Text = "";
        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "LIST")
            {
                status = true;
            }
            else if (this.action == "SEARCH")
            {
                status = false;
            }
            this.btnThem.Enabled = status;
            this.btnXoa.Enabled = status;
            this.btnSua.Enabled = status;
        }

     
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if(txtTimKiem.Text!="")
                dgvDSThuoc.DataSource = Thuoc.getDanhSachThuoc(txtTimKiem.Text);
        }

        private void txtTimKiem_Click(object sender, EventArgs e)
        {
            txtTimKiem.SelectAll();
        }

      

    }
}
